package IO;

import java.io.File;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-08-27
 * Time: 15:13
 */
public class Demo9 {
    private static void scan(File file,String key) {
        if(!file.isDirectory()) {
            return;
        }
        File[] files = file.listFiles();
        if(files == null || files.length == 0) {
            return;
        }
        for(File f : files) {
            if(f.isFile()) {
                dodelete(f,key);
            }else {
                scan(f,key);
            }
        }
    }

    private static void dodelete(File f, String key) {
        if(!f.getName().contains(key)) {
            return;
        }
        System.out.print(f.getAbsolutePath() + "确定要删除文件吗?");
        System.out.println(" " + "确定输入：Y/y 不确定输入： N/n ");
        Scanner scanner = new Scanner(System.in);
        String choice = scanner.next();
        if(choice.equals("y") || choice.equals("Y")) {
            f.delete();
        }
    }

    public static void main(String[] args) {
        System.out.print("请输入你要删除的文件路径: ");
        Scanner scanner = new Scanner(System.in);
        String rootpath = scanner.next();
        System.out.print("请输入你要删除的文件名：");
        String key = scanner.next();
        File file = new File(rootpath);
        if(!file.isDirectory()) {
            System.out.println("输入有误");
            return;
        }
        scan(file,key);
    }
}
